package h0908;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 最长连续序列
 * @date 2024/9/8 16:07
 */
public class LongestContinuousSequence {
    public static void main(String[] args) {
        int[] nums = {100, 4, 200, 3, 2, 1};
        System.out.println(longestConsecutive(nums));
    }

    public static int longestConsecutive(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
        }
        int maxLen = 0;
        //遍历集合，不能遍历数组，遍历数组速度很慢。
        for (Integer num : set) {
            int a = num;
            if (!set.contains(a-1)){
                int count = 0;
                while (set.contains(a+1)) {
                    count++;
                    a++;
                }
                maxLen = Math.max(maxLen, count+1);
            }
        }
        return maxLen;
    }
}
